package com.ruoyi.project.beautyflower.repaymentPlan.controller;

import java.util.List;

import com.ruoyi.project.app.interfaces.BigDecimalHandleInterface;
import com.ruoyi.project.beautyflower.order.domain.Order;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.beautyflower.repaymentPlan.domain.RepaymentPlan;
import com.ruoyi.project.beautyflower.repaymentPlan.service.IRepaymentPlanService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;

/**
 * 还款计划 信息操作处理
 *
 * @author lxs
 * @date 2018-10-16
 */
@Controller
@RequestMapping("/beautyflower/repaymentPlan")
public class RepaymentPlanController extends BaseController implements BigDecimalHandleInterface {
    private String prefix = "beautyflower/repaymentPlan";

    @Autowired
    private IRepaymentPlanService repaymentPlanService;

    @RequiresPermissions("beautyflower:repaymentPlan:view")
    @GetMapping()
    public String repaymentPlan() {
        return prefix + "/repaymentPlan";
    }

    /**
     * 查询还款计划列表
     */
    @RequiresPermissions("beautyflower:repaymentPlan:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RepaymentPlan repaymentPlan) {
        startPage();
        List<RepaymentPlan> list = repaymentPlanService.selectRepaymentPlanList(repaymentPlan);
        return getDataTable(list);
    }

    /**
     * 新增还款计划
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存还款计划
     */
    @RequiresPermissions("beautyflower:repaymentPlan:add")
    @Log(title = "还款计划", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RepaymentPlan repaymentPlan) {
        return toAjax(repaymentPlanService.insertRepaymentPlan(repaymentPlan));
    }

    /**
     * 修改还款计划
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        RepaymentPlan repaymentPlan = repaymentPlanService.selectRepaymentPlanById(id);
        mmap.put("repaymentPlan", repaymentPlan);
        return prefix + "/edit";
    }

    /**
     * 修改保存还款计划
     */
    @RequiresPermissions("beautyflower:repaymentPlan:edit")
    @Log(title = "还款计划", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RepaymentPlan repaymentPlan) {
        return toAjax(repaymentPlanService.updateRepaymentPlan(repaymentPlan));
    }

    /**
     * 删除还款计划
     */
    @RequiresPermissions("beautyflower:repaymentPlan:remove")
    @Log(title = "还款计划", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(repaymentPlanService.deleteRepaymentPlanByIds(ids));
    }

    /**
     * 修改订单
     */
    @GetMapping("/plandetails")
    public String planDetails(RepaymentPlan repaymentPlan, ModelMap mmap) {
        List<RepaymentPlan> repaymentPlans = repaymentPlanService.selectRepaymentPlanList(repaymentPlan);
        // 计算总利息
        String dueInterestAmount = "0";
        if (repaymentPlans != null) {
            for (RepaymentPlan plan : repaymentPlans) {
                if (plan.getDueInterestAmount() != null) {
                    dueInterestAmount = handleAddStr(dueInterestAmount, plan.getDueInterestAmount().toString(), 2);
                }
            }
        }
        mmap.put("dueInterestAmount", dueInterestAmount);
        mmap.put("plans", repaymentPlans);
        return prefix + "/plandetails";
    }
}
